﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于布卷剪布
    /// 搜索大布卷，列出所有小布卷，填入重量，确定，转移到指定库位和托盘
    /// </summary>
    public partial class TRFWB08
    {

        public TRFWB08()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent(); 
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
        }
        public override void Query_KeyUp(object sender, KeyEventArgs e)
        {
            base.Query_KeyUp(sender, e);
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            TBMain.Clear();
            if (STKNR.Length < 12)
                return;
            dwData = TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBSTKNR"] = STKNR;
            SResult = TMV.QRWBBS13(dwData);
            MergeData(SResult, true);
            //base.QueryCommand(sender, e);
        } 
        public override void OnCKGChg()
        {  
            if (dwMaster == null)
                return;
            if(CKG==0)
            {
              
                TMV.UpdateWBBST(dwMaster, 0);
                TMV.MOVE(dwMaster, LO, LB, LP, this.RUNID);
                IsPrint = true;
                return;
            } 
            TMV.WBBSGEW = CKG; 
            base.OnCKGChg();
        }
        public override void PrintChg()
        {
            if (!IsPrint)
                return;
            IsPrint = false;
            bool IsFound = false;
            if (dwMaster == null)
                return;
            DataTable tb = TBMain.Clone();

            DataRow _dwWB = tb.Rows.Add();
            _dwWB.ItemArray = dwMaster.ItemArray;
            DataSet Set = new DataSet();
            Set.Merge(tb);
            switch (app.company)
            {
                default:
                case "JLT":
                    foreach (DataRow row in tb.Rows)
                    {
                        string PACKNR = row["WBBPACKNR"].ToString();
                        string art = row["WBBART"].ToString();
                        string dr = @"C:\Program Files\mabu\templates";
                        string filename = "JLB" + art.Substring(3, 4) + ".xls";
                        if (!System.IO.Directory.Exists(dr))
                        {
                            IsFound = false;
                        }
                        else
                        {
                            string[] files = System.IO.Directory.GetFiles(dr);
                            foreach (string file in files)
                            {
                                if (!file.Contains(filename)) continue; IsFound = true; cDR.ExTemplate = file; break;
                            }
                            if (!IsFound)
                            {
                                filename = "coillabel" + art.Substring(3, 4) + ".xls";
                                foreach (string file in files)
                                {
                                    if (!file.Contains(filename)) continue; IsFound = true; cDR.ExTemplate = file; break;
                                }
                            }
                        }
                    }
                    if (IsFound)
                        cDR.PRINT(Set);
                    break;
            }
            vwMaster.View.MoveCurrentToNext();
        }
        private void PrintLBCommand(object sender, RoutedEventArgs e)
        {
            IsPrint = false;
            IsPrint = true;
        }

    }
}
